package org.lipert.rccalc.fragments;

import java.text.DecimalFormat;

import org.lipert.rccalc.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class ChargingFragment extends SherlockFragment implements
		OnClickListener {

	private Button calculateChargingButton;
	private TextView charginResultTextView;
	private EditText chargingCapacity;
	private EditText chargingCurrent;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.charging_layout, container, false);

		calculateChargingButton = (Button) view
				.findViewById(R.id.charging_calculate_button);
		calculateChargingButton.setOnClickListener(this);

		charginResultTextView = (TextView) view
				.findViewById(R.id.charging_result_text_view);
		chargingCapacity = (EditText) view
				.findViewById(R.id.charging_capacity_edit_text);
		chargingCurrent = (EditText) view
				.findViewById(R.id.charging_current_edit_text);
		return view;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == calculateChargingButton.getId()) {
			Float current = (float) Integer
					.parseInt(chargingCurrent.getText().toString());
			Float capacity = (float) Integer.parseInt(chargingCapacity.getText()
					.toString());
			Float result = capacity / current;
			DecimalFormat form = new DecimalFormat("0.00"); 
			charginResultTextView.setText(form.format(result).toString() + " h");

		}
	}
}
